package com.wechat.server.mp.controller;


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.wechat.server.mp.config.WechatConfigProperties;
import com.wechat.server.mp.util.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.apache.logging.log4j.util.PropertiesUtil;
import org.apache.tomcat.util.http.ResponseUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 自定义菜单
 * @author stoken
 * @date 2016/12/27
 */
@RequestMapping("/menu")
@RestController
@RequiredArgsConstructor
@Slf4j
public class MenuController {
    protected final WxMpServiceImpl wxMpService;
    protected final WechatConfigProperties properties;

    /**
     * 获取自定义菜单
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public R getMenu() {
        try {
            return R.ok().put("data", wxMpService.getMenuService().menuGet());
        } catch (WxErrorException e) {
            log.error("error", e);
            return R.error(e.getMessage());
        }
    }

    /**
     * 创建自定义菜单
     *
     * @param wxMenu
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public R createMenu(@RequestBody WxMenu wxMenu) {
        try {
            wxMpService.getMenuService().menuCreate(wxMenu);
            return R.ok();
        } catch (WxErrorException e) {
            log.error("error", e);
            return R.error(e.getMessage());
        }
    }

    /**
     * 从配置文件中创建自定义菜单
     *
     * @return
     */
    @RequestMapping(value = "/default", method = RequestMethod.POST)
    public R createMenu() {
        try {
            WxMenu wxMenu = WxMenu.fromJson("");
            wxMpService.getMenuService().menuCreate(wxMenu);
            return R.ok();
        } catch (WxErrorException e) {
            log.error("error", e);
            return R.error(e.getMessage());
        } catch (Exception e) {
            log.error("error", e);
            return R.error(e.getMessage());
        }
    }

    /**
     * 删除自定义菜单
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public R deleteMenu() {
        try {
            wxMpService.getMenuService().menuDelete();
            return R.ok();
        } catch (WxErrorException e) {
            log.error("error", e);
            return R.error(e.getMessage());
        }
    }

    @GetMapping("/json")
    public JSONObject json() {
        JSONObject jsonObject = JSONUtil.parseObj(menuStr);
        return jsonObject;

    }

    public static final String menuStr = "{\\\n" +
            "              \"button\": [\\\n" +
            "                  {\\\n" +
            "                      \"name\": \"\\u5C3E\\u7259\\u5E74\\u4F1A\",\\\n" +
            "                      \"sub_button\": [\\\n" +
            "                          {\\\n" +
            "                              \"type\": \"click\",\\\n" +
            "                              \"name\": \"\\u7B7E\\u5230\",\\\n" +
            "                              \"key\": \"{'type':'news','content':'\\u7B7E\\u5230\\u8868\\u5355'}\"\\\n" +
            "                          },\\\n" +
            "                          {\\\n" +
            "                              \"type\": \"click\",\\\n" +
            "                              \"name\": \"\\u8BC4\\u8BBA\\u4E0A\\u5899\",\\\n" +
            "                              \"key\": \"{'type':'news','content':'\\u53D1\\u9001\\u5F39\\u5E55'}\"\\\n" +
            "                          },\\\n" +
            "                          {\\\n" +
            "                              \"type\": \"click\",\\\n" +
            "                              \"name\": \"\\u6295\\u7968\",\\\n" +
            "                              \"key\": \"{'type':'news','content':'\\u6295\\u7968'}\"\\\n" +
            "                          }\\\n" +
            "                      ]\\\n" +
            "                  }\\\n" +
            "              ]\\\n" +
            "          }";
}
